package com.element.oauth2.exception;

import com.common.core.exception.code.BaseErrorCode;
import com.common.core.exception.code.SecurityErrorCode;
import org.springframework.lang.NonNull;
import org.springframework.security.oauth2.core.OAuth2AuthenticationException;
import org.springframework.security.oauth2.core.OAuth2Error;

public class ClientAuthenticationException extends OAuth2AuthenticationException {

    public ClientAuthenticationException(OAuth2Error error, @NonNull Throwable cause) {
        super(error, cause);
    }

    public static ClientAuthenticationException CLIENT_ERROR(String errMsg) {
        OAuth2Error error = new OAuth2Error(Integer.toString(SecurityErrorCode.A_CLIENT_AUTHENTICATION_FAILED.getErrNum()), errMsg, null);
        return new ClientAuthenticationException(error, new RuntimeException(errMsg));
    }

    public static ClientAuthenticationException ERROR(BaseErrorCode errorCode) {
        OAuth2Error error = new OAuth2Error(Integer.toString(errorCode.getErrNum()), errorCode.getErrMsg(), null);
        return new ClientAuthenticationException(error, new RuntimeException(errorCode.getErrMsg()));
    }

    public static ClientAuthenticationException ERROR(BaseErrorCode errorCode, String msg) {
        OAuth2Error error = new OAuth2Error(Integer.toString(errorCode.getErrNum()), msg, null);
        return new ClientAuthenticationException(error, new RuntimeException(errorCode.getErrMsg()));
    }

    public static ClientAuthenticationException ERROR(BaseErrorCode errorCode, Throwable e) {
        OAuth2Error error = new OAuth2Error(Integer.toString(errorCode.getErrNum()), errorCode.getErrMsg(), null);
        return new ClientAuthenticationException(error, e);
    }
}